<?php
namespace app\api;
use think\Cache;
use think\Model;
use think\Request;


class BaseModel extends Model
{
    public $request;
    public $requestData;
    protected $user;
    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->request = Request::instance();
        $this->requestData = empty($data)?$this->request->param(''):$data;
        if($this->request->__get('user')){
            $this->user = $this->request->__get('user');
        }
        if(isset($this->ParameterDetection)){
            call_user_func([$this, 'checkData']);
        }
    }

    /**
     * 路由检测以及参数是否已传
     */
    public function checkData()
    {
        $path_info = $_SERVER['PATH_INFO'];
        $path = explode('/', $path_info);
        if(count($path) > 3)die(json_encode(['data'=>[],'code'=>'-1','message'=>'地址不合法']));
        if(isset($this->ParameterDetection[$path[2]])){
            foreach($this->ParameterDetection[$path[2]] as $k => $v){
                if(!array_key_exists($v,$this->requestData))die(json_encode(['data'=>[],'code'=>'-1','message'=>$v.'参数未传']));
            }
        }
    }


}